package com.cjb.cloudfabric.util;

import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.TextAlignment;
import javafx.scene.text.FontWeight;
import com.itextpdf.layout.property.TextAlignment;
import java.io.ByteArrayOutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class PDFUtil
 * @Date 2023-11-01 下午11:37
 */
public class PDFUtil {
    /**
     * 创建PDF文档
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    public static byte[] createPdf(String text) throws Exception {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        PdfWriter writer = new PdfWriter(stream);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);

        // 创建字体
        PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);

        // 设置PDF的元数据
        pdf.getDocumentInfo().setTitle("数字证书");
        pdf.getDocumentInfo().setAuthor("cjb");

        // 添加标题
        Paragraph title = new Paragraph("公钥如下：")
                .setFont(font)
                .setFontSize(18)
                .setBold()
                .setTextAlignment(TextAlignment.CENTER);
        document.add(title);

        // 添加时间
        Paragraph date = new Paragraph(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()))
                .setFont(font)
                .setFontSize(14)
                .setTextAlignment(TextAlignment.CENTER);
        document.add(date);

        // 添加正文
        Paragraph body = new Paragraph(text)
                .setFont(font)
                .setFontSize(12)
                .setTextAlignment(TextAlignment.JUSTIFIED);
        document.add(body);

        document.close();

        return stream.toByteArray();
    }



}
